package jobhunter.controller;

import java.io.UnsupportedEncodingException;

import jobhunter.model.Process;
import jobhunter.model.User;
import jobhunter.repository.CompanyRepository;
import jobhunter.repository.ProcessRepository;
import jobhunter.service.ProcessService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class ProcessController 
{
	@Autowired
	private ProcessRepository processRepository;
	@Autowired
	private ProcessService processService;
	@Autowired
	private CompanyRepository companyRepository;

	@RequestMapping("/processesList")
	public ModelAndView processesList()
	{
		ModelAndView mav = new ModelAndView("process/processesList");
		mav.addObject("processesList", processRepository.findByUser());
		return mav;
	}


	@RequestMapping("/process")
	public ModelAndView contact(@RequestParam(value = "id") Long id,@RequestParam(value = "e", required = false) Long e)
	{
		Process process = processRepository.findByIdWithActions(id);	
		ModelAndView mav = new ModelAndView("process/process");
		mav.addObject("process",process);
		mav.addObject("typeError",e);
		return mav;
	}

	@RequestMapping("/delProcess")
	public String delContact(@RequestParam(value = "id") Long id,
			@RequestParam(value = "compId", required = false) Long compId)
	{
		try
		{
			processRepository.remove(id);
			if(compId != null )
				return "redirect:company?id="+compId;
			else
				return "redirect:processesList";			
		}
		catch(Exception e)
		{
			return "redirect:process?id="+id+"&e=1";
		}
		
		
	}
	
	@RequestMapping("/addProcess")
	public ModelAndView addContact(@RequestParam(value = "compId", required = false) Long compId)
	{
		ModelAndView mav = new ModelAndView("process/addProcess");
		
		if(compId != null)
			mav.addObject("company",companyRepository.findById(compId));
		else
			mav.addObject("companies",companyRepository.findByUser());				
		return mav;
	}

	@RequestMapping("/addProcessValidate")
	public String addContactValidate(@RequestParam(value = "name") String name,
			@RequestParam(value = "description") String description,
			@RequestParam(value = "companyId") Long companyId,
			@RequestParam(value = "page", required = false) String page) throws UnsupportedEncodingException
	{		
		
		name = new String(name.getBytes("ISO-8859-1"),"UTF-8");
		description = new String(description.getBytes("ISO-8859-1"),"UTF-8"); 
		
		
		processService.add(name,description,companyId);
		
		if(page==null)
			return "redirect:processesList";
		else
			return "redirect:company?id="+companyId;
	}



	@RequestMapping("/updProcess")
	public ModelAndView updContact(@RequestParam(value = "id") Long id,
			@RequestParam(value = "compId", required = false) Long compId)
	{
		ModelAndView mav = new ModelAndView("process/updProcess");
		mav.addObject("process",processRepository.findById(id));
		if(compId != null)
			mav.addObject("company",companyRepository.findById(compId));
		else
			mav.addObject("companies",companyRepository.findByUser());				
		return mav;	
		
	}

	@RequestMapping("/updProcessValidate")
	public String updContactValidate(@RequestParam(value = "id") Long id,
			@RequestParam(value = "name") String name,
			@RequestParam(value = "description") String description,
			@RequestParam(value = "companyId") Long companyId,
			@RequestParam(value = "page", required = false) String page) throws UnsupportedEncodingException
	{

		name = new String(name.getBytes("ISO-8859-1"),"UTF-8");
		description = new String(description.getBytes("ISO-8859-1"),"UTF-8");
		
		processService.update(id,name,description,companyId);
		if(page==null)
			return "redirect:processesList";
		else
			return "redirect:company?id="+companyId;
	}
}